/*
 * Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package br.com.lumis.saas.aws;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Logger;

import br.com.lumis.saas.util.Configuration;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;


/**
 * This is a class for storage of any kind of data on S3.  There is some functionality included in this
 * class that's not used in the TravelLog application but should serve to illustrate additional
 * capabilities of S3.
 *
 */
public class S3StorageManager {

	private Date lastUpdate;

	/*
	 * The s3 client class is thread safe so we only ever need one static instance.
	 * While you can have multiple instances it is better to only have one because it's
	 * a relatively heavy weight class.
	 */
	private static AmazonS3Client s3client;
	static {
		AWSCredentials creds = new BasicAWSCredentials(getKey(), getSecret());
		s3client = new AmazonS3Client(creds);
	}

	/**
	 * The bucket map keeps track of whether a bucket exists or not.  The first time
	 * any bucket name is called, it will be checked against this map and created
	 * if not already available.
	 */
	private static Map<String,Boolean> bucketMap = new LinkedHashMap<String,Boolean>();

	public Date getLastUpdate() {
		return lastUpdate;
	}

	private static final Logger logger = Logger.getLogger(S3StorageManager.class.getName());


	public AmazonS3Client getS3Client(){
		return s3client;		
	}

	

	public static String getKey () {
		Configuration config = Configuration.getInstance();
		return config.getProperty("accessKey");
	}

	public static String getSecret () {
		Configuration config = Configuration.getInstance();
		return config.getProperty("secretKey");
	}

}
